#include "move.h"

Move::Move()
{
    _x1 = 0, _y1 = 0, _x2 = 0, _y2 = 0;
    _specialRule = NONE;
}

Move::Move(int x1, int y1, int x2, int y2, SpecialRuleType specialRule){
    _x1 = x1;
    _y1 = y1;
    _x2 = x2;
    _y2 = y2;
    _specialRule = specialRule;
}

Move::Move(int x1, int y1, int x2, int y2){
    _x1 = x1;
    _y1 = y1;
    _x2 = x2;
    _y2 = y2;
    _specialRule = NONE;
}

Move::Move(SpecialRuleType specialRule){
    _specialRule = specialRule;
    _x1 = 0, _y1 = 0, _x2 = 0, _y2 = 0;
}

Move::~Move()
{
    //dtor
}

void Move::strToMove(const char* str)
{
    if(str[0] == '0' ) { // Castle
        if(strlen(str) == 3)
            _specialRule = CASTLESHORT;
        else
            _specialRule = CASTLELONG;
    } else {
        _x1 = charToInt(str[0]);
        _y1 = charToInt(str[1]);
        // str[2] is '-'
        _x2 = charToInt(str[3]);
        _y2 = charToInt(str[4]);

        if(strlen(str) > 5) {
            switch(str[5]) {
                case 'Q': _specialRule = QUEEN; break;
                case 'R': _specialRule = ROOK; break;
                case 'B': _specialRule = BISHOP; break;
                case 'K': _specialRule = KNIGHT; break;
                case 'e': _specialRule = PASSAT;
            }
        }
    }
}

void Move::print() const
{
    if(_specialRule == CASTLESHORT) {
        std::cout << "0-0";
    } else if(_specialRule == CASTLELONG) {
        std::cout << "0-0-0";
    } else {
        std::cout << intToChar(_x1) << (8-_y1) << "-";
        std::cout << intToChar(_x2) << (8-_y2);
        if(_specialRule == PASSAT){
            std::cout << "ep";
        } else if(_specialRule == QUEEN) {
            std::cout << "Q";
        } else if(_specialRule == ROOK) {
            std::cout << "R";
        } else if(_specialRule == BISHOP) {
            std::cout << "B";
        } else if(_specialRule == KNIGHT) {
            std::cout << "K";
        }
    }
}

int Move::getX1() const
{
    return _x1;
}

int Move::getY1() const
{
    return _y1;
}

int Move::getX2() const
{
    return _x2;
}

int Move::getY2() const
{
    return _y2;
}

int Move::getSpecialRule() const
{
    return _specialRule;
}

// Used for parsing strings in strToMove
int Move::charToInt(char c) const
{
    switch(c) {
        case 'a':
        case '8':
            return 0;
        case 'b':
        case '7':
            return 1;
        case 'c':
        case '6':
            return 2;
        case 'd':
        case '5':
            return 3;
        case 'e':
        case '4':
            return 4;
        case 'f':
        case '3':
            return 5;
        case 'g':
        case '2':
            return 6;
        case 'h':
        case '1':
            return 7;
        default:
            return -1;
    }
}

// Used in print()
char Move::intToChar(int i) const
{
    switch(i) {
        case 0: return 'a';
        case 1: return 'b';
        case 2: return 'c';
        case 3: return 'd';
        case 4: return 'e';
        case 5: return 'f';
        case 6: return 'g';
        case 7: return 'h';
        default: return '?';
    }
}
